home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / macintosh-c / macc-carbon-demos-nonbinhex.sit / macc-carbon-demos-nonbinhex / chap19-demo-carbon events / MoreResources.c < prev    next >
C/C++ Source or Header  |  2001-05-12  |  36KB  |  1,255 lines

  1. // *******************************************************************************************
  2. // MoreResources.c                                                          CARBON EVENT MODEL
  3. // *******************************************************************************************
  4. // 
  5. // This program uses custom resources to:
  6. //
  7. // •    Store application preferences in the resource fork of a Preferences file.
  8. //
  9. // •    Store, in the resource fork of a document file:
  10. //
  11. //        •  The size and position of the window associated with the document.
  12. //
  13. //        •  A flattened PMPageFormat object containing information about how the pages of the
  14. //             document should be printed, for example, on what paper size, in what printable
  15. //             area, and in what orientation (landscape or portrait). 
  16. //
  17. // The program also demonstrates setting, storing, and retrieving application preferences 
  18. // using Core Foundation Preferences Services.
  19. //
  20. // The program utilises the following standard resources:
  21. //
  22. // •    A 'plst' resource.
  23. //
  24. // •    An 'MBAR' resource, and 'MENU' resources for OS9Apple/Applcation, File, Edit and 
  25. //        Demonstration menus (preload, non-purgeable).  
  26. //
  27. // •    A 'DLOG' resource (purgeable) and associated 'dlgx', 'DITL' and 'CNTL' resources 
  28. //        (purgeable) associated with the display of, and user modification of, current 
  29. //        application preferences. 
  30. //
  31. // •    A 'STR#' resource (purgeable) containing the required name of the preferences file
  32. //        created by the program.
  33. //
  34. // •    A 'STR ' resource (purgeable) containing the application-missing string, which is copied
  35. //        to the resource fork of the preferences file.
  36. //
  37. // •    A 'SIZE' resource with the acceptSuspendResumeEvents, canBackground, 
  38. //        doesActivateOnFGSwitch, and isHighLevelEventAware flags set.
  39. //
  40. // The program creates and utilises the following custom resources:
  41. //
  42. // •    A 'PrFn' (preferences) resource comprising three boolean values, which is located in the
  43. //        program's resource file, which contains default preference values, and which is copied 
  44. //        to the resource fork of a preferences file created when the program is run for the first
  45. //        time.  Thereafter, the 'PrFn' resource in the preferences file is used for the storage
  46. //        and retrieval of application preferences set by the user.
  47. //
  48. // •    A 'WiSp' (window size and position) resource, which is created in the resource fork of
  49. //        the document file used by the program, and which is used to store the associated 
  50. //        window's port rectangle converted to global coordinates.
  51. //
  52. // •    A 'PgFt' (page format) resource, which is created in the resource fork of the document
  53. //        file used by the program, and which is used to store a flattened PMPageFormat object.
  54. //
  55. // *******************************************************************************************
  56.  
  57. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  58.  
  59. #include <Carbon.h>
  60.  
  61. // …………………………………………………………………………………………………………………………………………………………………………………………………………………………… defines
  62.  
  63. #define rMenubar                    128
  64. #define mAppleApplication    128
  65. #define  iAbout                        1
  66. #define mFile                            129
  67. #define  iOpen                        2
  68. #define  iClose                        4
  69. #define  iPageSetup                9
  70. #define  iQuit                        12
  71. #define mEdit                            130
  72. #define  iPreferences            10
  73. #define rPrefsDialog            128
  74. #define  iSound                        4    
  75. #define  iFullScreen            5
  76. #define  iAutoScroll            6
  77. #define rStringList                128
  78. #define  iPrefsFileName        1
  79. #define rTypePrefs                'PrFn'
  80. #define  kPrefsID                    128
  81. #define rTypeWinSizePos        'WiSp'
  82. #define  kWinSizePosID        128
  83. #define rPageFormat                'PgFt'
  84. #define  kPageFormatID        128
  85. #define rTypeAppMiss            'STR '
  86. #define  kAppMissID                -16397
  87. #define topLeft(r)                (((Point *) &(r))[0])
  88. #define botRight(r)                (((Point *) &(r))[1])
  89.  
  90. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… typedefs
  91.  
  92. typedef struct
  93. {
  94.     Boolean    sound;
  95.     Boolean    fullScreen;
  96.     Boolean    autoScroll;
  97.     SInt16    programRunCount;
  98. } appPrefs, **appPrefsHandle;
  99.  
  100. typedef struct
  101. {
  102.     Rect windowRect;
  103. } windowSizePos, **windowSizePosHandle;
  104.  
  105. typedef struct
  106. {
  107.     Handle    pageFormatHdl;
  108.     FSSpec    fileFSSpec;
  109. } docStructure, **docStructureHandle;
  110.  
  111. // …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
  112.  
  113. Boolean                    gRunningOnX = false;
  114. PMPrintSession    gPrintSession;
  115. WindowRef                gWindowRef;
  116. SInt16                    gAppResFileRefNum;
  117. Boolean                    gSoundPref;
  118. Boolean                    gFullScreenPref;
  119. Boolean                    gAutoScrollPref;    
  120. SInt16                    gProgramRunCountPref;
  121. Boolean                    gSoundCFPref;
  122. Boolean                    gFullScreenCFPref;
  123. Boolean                    gAutoScrollCFPref;
  124. SInt16                    gProgramRunCountCFPref;
  125. Boolean                    gWindowOpen                    = false;
  126. Boolean                    gPageFormatChanged    = false;
  127. SInt16                    gPrefsFileRefNum        = 0;
  128.  
  129. // …………………………………………………………………………………………………………………………………………………………………………………………… function prototypes
  130.  
  131. void            main                                                            (void);
  132. void            doPreliminaries                                        (void);
  133. OSStatus    appEventHandler                                        (EventHandlerCallRef,EventRef,void *);
  134. OSStatus    windowEventHandler                                (EventHandlerCallRef,EventRef,void *);
  135. void            doDrawContent                                            (WindowRef);
  136. void            doAdjustMenus                                            (void);
  137. void            doMenuChoice                                            (MenuID,MenuItemIndex);
  138. void            doErrorAlert                                            (SInt16);
  139. void            doOpenCommand                                            (void);
  140. void            navEventFunction                                    (NavEventCallbackMessage,NavCBRecPtr,
  141.                                                                                          NavCallBackUserData);
  142. void            doOpenWindow                                            (FSSpec);
  143. void            doCloseWindow                                            (void);
  144. void            doPreferencesDialog                                (void);
  145. OSStatus    doPageSetupDialog                                    (void);
  146. void            doGetPreferencesResource                    (void);
  147. void            doGetPreferencesCFPrefs                        (void);
  148. OSErr            doCopyResource                                        (ResType,SInt16,SInt16,SInt16);
  149. void            doSavePreferencesResource                    (void);
  150. void            doSavePreferencesCFPrefs                    (void);
  151. void            doLoadAndSetWindowSizeAndPosition    (WindowRef);
  152. void            doGetFrameWidthAndTitleBarHeight    (WindowRef,SInt16 *,SInt16 *);
  153. void            doSaveWindowSizeAndPosition                (WindowRef);
  154. void            doGetPageFormat                                        (WindowRef);
  155. void            doSavePageFormat                                    (WindowRef);
  156.  
  157. // ************************************************************************************** main
  158.  
  159. void  main(void)
  160. {
  161.     MenuBarHandle    menubarHdl;
  162.     SInt32                response;
  163.     MenuRef                menuRef;
  164.     OSStatus            osStatus;
  165.     EventTypeSpec    applicationEvents[] =    {    { kEventClassCommand, kEventProcessCommand  },
  166.                                                                                 { kEventClassMenu,    kEventMenuEnableItems } };
  167.  
  168.     // ……………………………………………………………………………………………………………………………………………………………………………………………… do preliminaries
  169.  
  170.     doPreliminaries();
  171.  
  172.     // ………………………………………………………………………………………… set current resource file to application resource fork
  173.  
  174.     gAppResFileRefNum = CurResFile();
  175.  
  176.     // ……………………………………………………………………………………………………………………………………………………………………… set up menu bar and menus
  177.  
  178.     menubarHdl = GetNewMBar(rMenubar);
  179.     if(menubarHdl == NULL)
  180.         doErrorAlert(MemError());
  181.     SetMenuBar(menubarHdl);
  182.     DrawMenuBar();
  183.  
  184.     Gestalt(gestaltMenuMgrAttr,&response);
  185.     if(response & gestaltMenuMgrAquaLayoutMask)
  186.     {
  187.         menuRef = GetMenuRef(mFile);
  188.         if(menuRef != NULL)
  189.         {
  190.             DeleteMenuItem(menuRef,iQuit);
  191.             DeleteMenuItem(menuRef,iQuit - 1);
  192.         }
  193.         menuRef = GetMenuRef(mEdit);
  194.         if(menuRef != NULL)
  195.         {
  196.             DeleteMenuItem(menuRef,iPreferences);
  197.             DeleteMenuItem(menuRef,iPreferences - 1);
  198.             DisableMenuItem(menuRef,0);
  199.         }
  200.     
  201.         EnableMenuCommand(NULL,kHICommandPreferences);
  202.  
  203.         gRunningOnX = true;
  204.     }
  205.     else
  206.     {
  207.         menuRef = GetMenuRef(mFile);
  208.         if(menuRef != NULL)
  209.             SetMenuItemCommandID(menuRef,iQuit,kHICommandQuit);
  210.  
  211.         menuRef = GetMenuRef(mEdit);
  212.         if(menuRef != NULL)
  213.             SetMenuItemCommandID(menuRef,iPreferences,kHICommandPreferences);
  214.     }
  215.  
  216.     // …………………………………………………………………………………………………………………………………………………………………………… create printing session
  217.  
  218.     osStatus = PMCreateSession(&gPrintSession);
  219.     if(osStatus != kPMNoError)
  220.         doErrorAlert(osStatus);
  221.  
  222.     // ………………………………………………………………… read in application preferences and increment program run count
  223.  
  224.     doGetPreferencesResource();
  225.     doGetPreferencesCFPrefs();
  226.  
  227.     gProgramRunCountPref++;
  228.     gProgramRunCountCFPref++;
  229.  
  230.     // ………………………………………………………………………………………………………………………………………………… install application event handler
  231.   
  232.     InstallApplicationEventHandler(NewEventHandlerUPP((EventHandlerProcPtr) appEventHandler),
  233.                                                                  GetEventTypeCount(applicationEvents),applicationEvents,
  234.                                                                  0,NULL);
  235.  
  236.     // …………………………………………………………………………………………………………………………………………………………………… run application event loop
  237.  
  238.     RunApplicationEventLoop();
  239. }
  240.  
  241. // *************************************************************************** doPreliminaries
  242.  
  243. void  doPreliminaries(void)
  244. {
  245.     MoreMasterPointers(320);
  246.     InitCursor();
  247. }
  248.  
  249. // *************************************************************************** appEventHandler
  250.  
  251. OSStatus  appEventHandler(EventHandlerCallRef eventHandlerCallRef,EventRef eventRef,
  252.                                                     void * userData)
  253. {
  254.     OSStatus            result = eventNotHandledErr;
  255.     UInt32                eventClass;
  256.     UInt32                eventKind;
  257.     HICommand            hiCommand;
  258.     MenuID                menuID;
  259.     MenuItemIndex    menuItem;
  260.  
  261.     eventClass = GetEventClass(eventRef);
  262.     eventKind  = GetEventKind(eventRef);
  263.  
  264.     switch(eventClass)
  265.     {
  266.         case kEventClassCommand:
  267.             if(eventKind == kEventProcessCommand)
  268.             {
  269.                 GetEventParameter(eventRef,kEventParamDirectObject,typeHICommand,NULL,
  270.                                                     sizeof(HICommand),NULL,&hiCommand);
  271.                 menuID = GetMenuID(hiCommand.menu.menuRef);
  272.                 menuItem = hiCommand.menu.menuItemIndex;
  273.                 if(hiCommand.commandID == kHICommandPreferences)
  274.                 {
  275.                     doPreferencesDialog();
  276.                     result = noErr;
  277.                 }
  278.                 else if(hiCommand.commandID == kHICommandQuit)
  279.                 {
  280.                     while(FrontWindow())
  281.                         doCloseWindow();
  282.                     PMRelease(&gPrintSession);
  283.                     doSavePreferencesResource();
  284.                     doSavePreferencesCFPrefs();
  285.                 }
  286.                 else if((menuID >= mAppleApplication && menuID <= mEdit))
  287.                 {
  288.                     doMenuChoice(menuID,menuItem);
  289.                     result = noErr;
  290.                 }
  291.             }
  292.             break;
  293.  
  294.         case kEventClassMenu:
  295.             if(eventKind == kEventMenuEnableItems)
  296.             {
  297.                 doAdjustMenus();
  298.                 result = noErr;
  299.             }
  300.             break;
  301.     }
  302.  
  303.     return result;
  304. }
  305.  
  306. // ************************************************************************ windowEventHandler
  307.  
  308. OSStatus  windowEventHandler(EventHandlerCallRef eventHandlerCallRef,EventRef eventRef,
  309.                                                          void* userData)
  310. {
  311.     OSStatus    result = eventNotHandledErr;
  312.     UInt32        eventClass;
  313.     UInt32        eventKind;
  314.     WindowRef    windowRef;
  315.     Rect            mainScreenRect;
  316.     BitMap        screenBits;
  317.     Point            idealHeightAndWidth, minimumHeightAndWidth;
  318.     
  319.     eventClass = GetEventClass(eventRef);
  320.     eventKind  = GetEventKind(eventRef);
  321.  
  322.     switch(eventClass)
  323.     {
  324.         case kEventClassWindow:
  325.             GetEventParameter(eventRef,kEventParamDirectObject,typeWindowRef,NULL,sizeof(windowRef),
  326.                                         NULL,&windowRef);
  327.             switch(eventKind)
  328.             {
  329.                 case kEventWindowDrawContent:
  330.                     doDrawContent(windowRef);
  331.                     result = noErr;
  332.                     break;
  333.  
  334.                 case kEventWindowGetIdealSize:
  335.                     mainScreenRect = GetQDGlobalsScreenBits(&screenBits)->bounds;
  336.                     idealHeightAndWidth.v = mainScreenRect.bottom - 75;
  337.                     idealHeightAndWidth.h = 600;
  338.                     SetEventParameter(eventRef,kEventParamDimensions,typeQDPoint,
  339.                                                         sizeof(idealHeightAndWidth),&idealHeightAndWidth);
  340.                      result = noErr;
  341.                     break;
  342.  
  343.                 case kEventWindowGetMinimumSize:
  344.                     minimumHeightAndWidth.v = 190; 
  345.                     minimumHeightAndWidth.h = 400;
  346.                     SetEventParameter(eventRef,kEventParamDimensions,typeQDPoint,
  347.                                                         sizeof(minimumHeightAndWidth),&minimumHeightAndWidth);
  348.                     result = noErr;
  349.                     break;
  350.  
  351.                 case kEventWindowClose:
  352.                     doCloseWindow();
  353.                     result = noErr;
  354.                     break;
  355.             }
  356.             break;
  357.     }
  358.  
  359.     return result;
  360. }
  361.  
  362. // ***************************************************************************** doDrawContent
  363.  
  364. void  doDrawContent(WindowRef windowRef)
  365. {
  366.     RGBColor                        whiteColour    = { 0xFFFF, 0xFFFF, 0xFFFF };
  367.     RGBColor                        blueColour    = { 0x1818, 0x4B4B, 0x8181 };
  368.     Rect                                portRect;
  369.     docStructureHandle    docStrucHdl;
  370.     PMPageFormat                pageFormat    = kPMNoPageFormat;
  371.     Str255                            string;
  372.     PMResolution                resolution;
  373.     PMRect                            paperRect;
  374.     PMRect                            pageRect;
  375.     UInt16                            orientation;
  376.  
  377.     RGBForeColor(&whiteColour);
  378.     RGBBackColor(&blueColour);
  379.     GetWindowPortBounds(windowRef,&portRect);
  380.     EraseRect(&portRect);    
  381.  
  382.     SetPortWindowPort(windowRef);
  383.  
  384.     MoveTo(10,20);
  385.     TextFace(bold);
  386.     DrawString("\pApplication Preferences:");
  387.     MoveTo(10,35);
  388.     DrawString("\pResource Fork Prefs File");
  389.     MoveTo(170,35);
  390.     DrawString("\pCF Preferences Services");
  391.     TextFace(normal);
  392.     MoveTo(10,50);
  393.     DrawString("\pSound On:  ");
  394.     if(gSoundPref)  DrawString("\pYES");
  395.     else  DrawString("\pNO");
  396.     MoveTo(10,65);
  397.     DrawString("\pFull Screen On:  ");
  398.     if(gFullScreenPref)  DrawString("\pYES");
  399.     else  DrawString("\pNO");
  400.     MoveTo(10,80);
  401.     DrawString("\pAutoScroll On:  ");
  402.     if(gAutoScrollPref)  DrawString("\pYES");
  403.     else  DrawString("\pNO");
  404.     MoveTo(10,95);
  405.     DrawString("\pProgram run count: ");
  406.     NumToString((SInt32) gProgramRunCountPref,string);
  407.     DrawString(string);
  408.  
  409.     MoveTo(170,50);
  410.     DrawString("\pSound On:  ");
  411.     if(gSoundCFPref)  DrawString("\pYES");
  412.     else  DrawString("\pNO");
  413.     MoveTo(170,65);
  414.     DrawString("\pFull Screen On:  ");
  415.     if(gFullScreenCFPref)  DrawString("\pYES");
  416.     else  DrawString("\pNO");
  417.     MoveTo(170,80);
  418.     DrawString("\pAutoScroll On:  ");
  419.     if(gAutoScrollCFPref)  DrawString("\pYES");
  420.     else  DrawString("\pNO");
  421.     MoveTo(170,95);
  422.     DrawString("\pProgram run count: ");
  423.     NumToString((SInt32) gProgramRunCountCFPref,string);
  424.     DrawString(string);
  425.  
  426.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  427.     PMUnflattenPageFormat((*docStrucHdl)->pageFormatHdl,&pageFormat);
  428.     PMGetResolution(pageFormat,&resolution);
  429.     PMGetAdjustedPaperRect(pageFormat,&paperRect);
  430.     PMGetAdjustedPageRect(pageFormat,&pageRect);
  431.     PMGetOrientation(pageFormat,&orientation);
  432.     if(pageFormat != kPMNoPageFormat)
  433.         PMRelease(&pageFormat);
  434.  
  435.     MoveTo(10,115);
  436.     TextFace(bold);
  437.     DrawString("\pInformation From Document's 'PgFt' (Page Format) Resource:"); 
  438.     TextFace(normal);
  439.  
  440.     if((*docStrucHdl)->pageFormatHdl != NULL)
  441.     {
  442.         MoveTo(10,130);
  443.         DrawString("\pApplication's Drawing Resolution:  ");
  444.         NumToString((long) resolution.hRes,string);
  445.         DrawString(string);
  446.         DrawString("\p dpi horizontal, ");
  447.         NumToString((long) resolution.vRes,string);
  448.         DrawString(string);
  449.         DrawString("\p dpi vertical");
  450.         
  451.         MoveTo(10,145);
  452.         DrawString("\pPaper Rectangle Size in Drawing Resolution:  ");
  453.         NumToString((long) (paperRect.bottom - paperRect.top),string);
  454.         DrawString(string);
  455.         DrawString("\p by ");
  456.         NumToString((long) (paperRect.right - paperRect.left),string);
  457.         DrawString(string);
  458.  
  459.         MoveTo(10,160);
  460.         DrawString("\pPage Rectangle Size in Drawing Resolution:  ");
  461.         NumToString((long) (pageRect.bottom - pageRect.top),string);
  462.         DrawString(string);
  463.         DrawString("\p by ");
  464.         NumToString((long) (pageRect.right - pageRect.left),string);
  465.         DrawString(string);
  466.  
  467.         MoveTo(10,175);
  468.         DrawString("\pOrientation:  ");
  469.         if(orientation == 1)
  470.             DrawString("\pPortrait");
  471.         else if(orientation == 2)
  472.             DrawString("\pLandscape");
  473.     }
  474.     else
  475.     {
  476.         MoveTo(10,130);
  477.         DrawString("\pA page format ('PgFt') resource has not been saved yet."); 
  478.         MoveTo(10,145);
  479.         DrawString("\pOpen the Page Setup... dialog before closing the window or quitting."); 
  480.     }
  481.  
  482.     QDFlushPortBuffer(GetWindowPort(gWindowRef),NULL);
  483. }
  484.  
  485. // ***************************************************************************** doAdjustMenus
  486.  
  487. void  doAdjustMenus(void)
  488. {
  489.     MenuRef    menuRef;
  490.  
  491.     if(gWindowOpen)
  492.     {
  493.         menuRef = GetMenuRef(mFile);
  494.         DisableMenuItem(menuRef,iOpen);
  495.         EnableMenuItem(menuRef,iClose);
  496.         EnableMenuItem(menuRef,iPageSetup);
  497.     }
  498.     else
  499.     {
  500.         menuRef = GetMenuRef(mFile);
  501.         EnableMenuItem(menuRef,iOpen);
  502.         DisableMenuItem(menuRef,iClose);
  503.         DisableMenuItem(menuRef,iPageSetup);
  504.     }
  505.  
  506.     DrawMenuBar();
  507. }
  508.  
  509. // ****************************************************************************** doMenuChoice
  510.  
  511. void  doMenuChoice(MenuID menuID,MenuItemIndex menuItem)
  512. {
  513.     OSStatus    osStatus;
  514.     Rect            portRect;
  515.  
  516.     if(menuID == 0)
  517.         return;
  518.  
  519.     switch(menuID)
  520.     {
  521.         case mAppleApplication:
  522.             if(menuItem == iAbout)
  523.                 SysBeep(10);
  524.             break;
  525.  
  526.         case mFile:
  527.             switch(menuItem)
  528.             {
  529.                 case iClose:
  530.                     doCloseWindow();
  531.                     break;
  532.  
  533.                 case iOpen:
  534.                     doOpenCommand();
  535.                     break;
  536.  
  537.                 case iPageSetup:
  538.                     osStatus = doPageSetupDialog();
  539.                     if(osStatus != kPMNoError && osStatus != kPMCancel)
  540.                         doErrorAlert(osStatus);
  541.                     if(FrontWindow())
  542.                     {
  543.                         GetWindowPortBounds(FrontWindow(),&portRect);
  544.                         InvalWindowRect(FrontWindow(),&portRect);
  545.                     }
  546.                     break;
  547.             }
  548.             break;
  549.     }
  550. }
  551.  
  552. // ****************************************************************************** doErrorAlert
  553.  
  554. void  doErrorAlert(SInt16 errorCode)
  555. {
  556.     Str255    errorString;
  557.     SInt16    itemHit;
  558.  
  559.     NumToString((SInt32) errorCode,errorString);
  560.  
  561.     if(errorCode != memFullErr)
  562.         StandardAlert(kAlertCautionAlert,errorString,NULL,NULL,&itemHit);
  563.     else
  564.     {
  565.         StandardAlert(kAlertStopAlert,errorString,NULL,NULL,&itemHit);
  566.         ExitToShell();
  567.     }
  568. }
  569.  
  570. // ***************************************************************************** doOpenCommand
  571.  
  572. void  doOpenCommand(void)
  573. {
  574.     OSErr                            osError = noErr;
  575.     NavDialogOptions    dialogOptions;
  576.     NavEventUPP                navEventFunctionUPP;    
  577.     NavReplyRecord        navReplyStruc;
  578.     SInt32                        index, count;
  579.     AEKeyword                    theKeyword;
  580.     DescType                    actualType;
  581.     FSSpec                        fileSpec;    
  582.     Size                            actualSize;
  583.  
  584.     osError = NavGetDefaultDialogOptions(&dialogOptions);
  585.  
  586.     if(osError == noErr)
  587.     {
  588.         navEventFunctionUPP = NewNavEventUPP((NavEventProcPtr) navEventFunction);
  589.         osError = NavGetFile(NULL,&navReplyStruc,&dialogOptions,navEventFunctionUPP,NULL,NULL,
  590.                                                  NULL,NULL);
  591.         DisposeNavEventUPP(navEventFunctionUPP);
  592.  
  593.         if(osError == noErr && navReplyStruc.validRecord)
  594.         {
  595.             osError = AECountItems(&(navReplyStruc.selection),&count);
  596.             if(osError == noErr)
  597.             {
  598.                 for(index=1;index<=count;index++)
  599.                 {
  600.                     osError = AEGetNthPtr(&(navReplyStruc.selection),index,typeFSS,&theKeyword,
  601.                                                                 &actualType,&fileSpec,sizeof(fileSpec),&actualSize);
  602.  
  603.                     doOpenWindow(fileSpec);
  604.                 }
  605.             }
  606.  
  607.             NavDisposeReply(&navReplyStruc);    
  608.         }
  609.     }
  610. }
  611.  
  612. // ************************************************************************** navEventFunction
  613.  
  614. void  navEventFunction(NavEventCallbackMessage callBackSelector,NavCBRecPtr callBackParms,
  615.                                              NavCallBackUserData callBackUD)
  616. {
  617. }
  618.  
  619. // ****************************************************************************** doOpenWindow
  620.  
  621. void  doOpenWindow(FSSpec fileSpec)
  622. {
  623.     OSStatus                        osError;
  624.     docStructureHandle    docStrucHdl;
  625.     Rect                                contentRect = { 100,100,290,500 };
  626.     WindowAttributes        attributes    = { kWindowStandardHandlerAttribute |
  627.                                                                             kWindowStandardDocumentAttributes };
  628.     EventTypeSpec                windowEvents[] = { { kEventClassWindow, kEventWindowDrawContent    },
  629.                                                                                  { kEventClassWindow, kEventWindowGetIdealSize   },
  630.                                                                                  { kEventClassWindow, kEventWindowGetMinimumSize },
  631.                                                                                  { kEventClassWindow, kEventWindowClose          } };
  632.  
  633.     osError = CreateNewWindow(kDocumentWindowClass,attributes,&contentRect,&gWindowRef);
  634.     if(osError != noErr)
  635.         QuitApplicationEventLoop();
  636.  
  637.     if(!(docStrucHdl = (docStructureHandle) NewHandle(sizeof(docStructure))))
  638.     {
  639.         DisposeWindow(gWindowRef);
  640.         QuitApplicationEventLoop();
  641.     }
  642.  
  643.     SetWRefCon(gWindowRef,(SInt32) docStrucHdl);
  644.     (*docStrucHdl)->fileFSSpec = fileSpec;
  645.     SetWTitle(gWindowRef,(*docStrucHdl)->fileFSSpec.name);    
  646.     (*docStrucHdl)->pageFormatHdl = NULL;
  647.  
  648.     SetPortWindowPort(gWindowRef);
  649.     UseThemeFont(kThemeSmallSystemFont,smSystemScript);
  650.  
  651.     InstallWindowEventHandler(gWindowRef,
  652.                                                         NewEventHandlerUPP((EventHandlerProcPtr) windowEventHandler),
  653.                                                         GetEventTypeCount(windowEvents),windowEvents,0,NULL);
  654.                     
  655.     doLoadAndSetWindowSizeAndPosition(gWindowRef);
  656.     doGetPageFormat(gWindowRef);
  657.     ShowWindow(gWindowRef);
  658.     gWindowOpen = true; 
  659. }
  660.  
  661. // ***************************************************************************** doCloseWindow
  662.  
  663. void  doCloseWindow(void)
  664. {
  665.     WindowRef                        windowRef;
  666.     docStructureHandle    docStrucHdl;
  667.     OSErr                                osError = 0;
  668.  
  669.     windowRef = FrontWindow();
  670.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  671.  
  672.     doSaveWindowSizeAndPosition(windowRef);
  673.  
  674.     if(gPageFormatChanged)
  675.         doSavePageFormat(windowRef);
  676.  
  677.     DisposeHandle((Handle) docStrucHdl);
  678.     DisposeWindow(windowRef);
  679.     gWindowOpen = false;
  680. }
  681.  
  682. // *********************************************************************** doPreferencesDialog
  683.  
  684. void  doPreferencesDialog(void)
  685. {
  686.     DialogRef     modalDlgRef;
  687.     ControlRef    controlHdl;
  688.     SInt16             itemHit;
  689.     Rect                portRect;
  690.  
  691.     if(!(modalDlgRef = GetNewDialog(rPrefsDialog,NULL,(WindowRef) -1)))
  692.         return;
  693.  
  694.     SetDialogDefaultItem(modalDlgRef,kStdOkItemIndex);
  695.     SetDialogCancelItem(modalDlgRef,kStdCancelItemIndex);
  696.  
  697.     GetDialogItemAsControl(modalDlgRef,iSound,&controlHdl);
  698.     SetControlValue(controlHdl,gSoundPref);
  699.     GetDialogItemAsControl(modalDlgRef,iFullScreen,&controlHdl);
  700.     SetControlValue(controlHdl,gFullScreenPref);
  701.     GetDialogItemAsControl(modalDlgRef,iAutoScroll,&controlHdl);
  702.     SetControlValue(controlHdl,gAutoScrollPref);
  703.  
  704.     ShowWindow(GetDialogWindow(modalDlgRef));
  705.  
  706.     do
  707.     {
  708.         ModalDialog(NULL,&itemHit);
  709.         GetDialogItemAsControl(modalDlgRef,itemHit,&controlHdl);
  710.         SetControlValue(controlHdl,!GetControlValue(controlHdl));
  711.     } while((itemHit != kStdOkItemIndex) && (itemHit != kStdCancelItemIndex));
  712.     
  713.     if(itemHit == kStdOkItemIndex)
  714.     {
  715.         GetDialogItemAsControl(modalDlgRef,iSound,&controlHdl);
  716.         gSoundPref = gSoundCFPref = GetControlValue(controlHdl);
  717.         GetDialogItemAsControl(modalDlgRef,iFullScreen,&controlHdl);
  718.         gFullScreenPref = gFullScreenCFPref = GetControlValue(controlHdl);
  719.         GetDialogItemAsControl(modalDlgRef,iAutoScroll,&controlHdl);
  720.         gAutoScrollPref = gAutoScrollCFPref = GetControlValue(controlHdl);                
  721.     }
  722.  
  723.     DisposeDialog(modalDlgRef);
  724.  
  725.     if(gWindowRef)
  726.     {
  727.         GetWindowPortBounds(gWindowRef,&portRect);
  728.         InvalWindowRect(gWindowRef,&portRect);
  729.     }
  730.  
  731.     doSavePreferencesResource();
  732.     doSavePreferencesCFPrefs();
  733. }
  734.  
  735. // ************************************************************************* doPageSetupDialog
  736.  
  737. OSStatus  doPageSetupDialog(void)
  738. {
  739.     docStructureHandle    docStrucHdl;
  740.     OSStatus                        osStatus        = kPMNoError;
  741.     PMPageFormat                pageFormat    = kPMNoPageFormat;
  742.     Boolean                            userClickedOKButton;
  743.  
  744.     docStrucHdl = (docStructureHandle) GetWRefCon(gWindowRef);    
  745.     HLock((Handle) docStrucHdl);
  746.  
  747.     if((*docStrucHdl)->pageFormatHdl == NULL)
  748.     {
  749.         osStatus = PMCreatePageFormat(&pageFormat);
  750.         if((osStatus == kPMNoError) && (pageFormat != kPMNoPageFormat))
  751.             osStatus = PMSessionDefaultPageFormat(gPrintSession,pageFormat);
  752.         if(osStatus == kPMNoError)
  753.             osStatus = PMFlattenPageFormat(pageFormat,&(*docStrucHdl)->pageFormatHdl);
  754.     }
  755.     else
  756.     {
  757.         osStatus = PMUnflattenPageFormat((*docStrucHdl)->pageFormatHdl,&pageFormat);
  758.         if(osStatus == kPMNoError)
  759.             osStatus = PMSessionValidatePageFormat(gPrintSession,pageFormat,kPMDontWantBoolean);
  760.     }
  761.  
  762.     if((osStatus == kPMNoError) && (pageFormat != kPMNoPageFormat))
  763.     {
  764.         SetThemeCursor(kThemeArrowCursor);
  765.  
  766.         osStatus = PMSessionPageSetupDialog(gPrintSession,pageFormat,&userClickedOKButton);
  767.         if(!userClickedOKButton)
  768.             osStatus = kPMCancel;
  769.     }
  770.  
  771.     if(osStatus == kPMNoError && userClickedOKButton)
  772.     {
  773.         DisposeHandle((*docStrucHdl)->pageFormatHdl);
  774.         (*docStrucHdl)->pageFormatHdl = NULL;
  775.         osStatus = PMFlattenPageFormat(pageFormat,&(*docStrucHdl)->pageFormatHdl);
  776.  
  777.         gPageFormatChanged = true;
  778.     }
  779.  
  780.     if(pageFormat != kPMNoPageFormat)
  781.         PMRelease(&pageFormat);
  782.  
  783.     HUnlock((Handle) docStrucHdl);
  784.  
  785.     return osStatus;
  786. }
  787.  
  788. // ****************************************************************** doGetPreferencesResource
  789.  
  790. void  doGetPreferencesResource(void)
  791. {
  792.     Str255                    prefsFileName;
  793.     OSErr                        osError;
  794.     SInt16                    volRefNum;
  795.     long                        directoryID;
  796.     FSSpec                     fileSSpec;
  797.     SInt16                    fileRefNum;
  798.     appPrefsHandle    appPrefsHdl;
  799.  
  800.     GetIndString(prefsFileName,rStringList,iPrefsFileName);
  801.  
  802.     osError = FindFolder(kUserDomain,kPreferencesFolderType,kDontCreateFolder,&volRefNum,
  803.                                              &directoryID);
  804.     if(osError == noErr)
  805.         osError = FSMakeFSSpec(volRefNum,directoryID,prefsFileName,&fileSSpec);
  806.     if(osError == noErr || osError == fnfErr)
  807.         fileRefNum = FSpOpenResFile(&fileSSpec,fsCurPerm);
  808.  
  809.     if(fileRefNum == -1)
  810.     {
  811.         FSpCreateResFile(&fileSSpec,'PpPp','pref',smSystemScript);
  812.         osError = ResError();
  813.  
  814.         if(osError == noErr)
  815.         {
  816.             fileRefNum = FSpOpenResFile(&fileSSpec,fsCurPerm);
  817.             if(fileRefNum != -1 )
  818.             {
  819.                 UseResFile(gAppResFileRefNum);
  820.                     
  821.                 osError = doCopyResource(rTypePrefs,kPrefsID,gAppResFileRefNum,fileRefNum);
  822.                 if(osError == noErr)
  823.                     osError = doCopyResource(rTypeAppMiss,kAppMissID,gAppResFileRefNum,fileRefNum);
  824.                 if(osError != noErr)
  825.                 {
  826.                     CloseResFile(fileRefNum);
  827.                     osError = FSpDelete(&fileSSpec);
  828.                     fileRefNum = -1;
  829.                 }
  830.             }
  831.         }
  832.     }
  833.  
  834.     if(fileRefNum != -1)
  835.     {
  836.         UseResFile(fileRefNum); 
  837.  
  838.         appPrefsHdl = (appPrefsHandle) Get1Resource(rTypePrefs,kPrefsID);
  839.         if(appPrefsHdl == NULL)
  840.             return;
  841.  
  842.         gSoundPref = (*appPrefsHdl)->sound;
  843.         gFullScreenPref = (*appPrefsHdl)->fullScreen;
  844.         gAutoScrollPref = (*appPrefsHdl)->autoScroll;
  845.         gProgramRunCountPref = (*appPrefsHdl)->programRunCount;        
  846.  
  847.         gPrefsFileRefNum = fileRefNum;
  848.  
  849.         UseResFile(gAppResFileRefNum);
  850.     }
  851. }
  852.  
  853. // ******************************************************************* doGetPreferencesCFPrefs
  854.  
  855. void  doGetPreferencesCFPrefs(void)
  856. {
  857.     CFStringRef    applicationID9 = CFSTR("MoreResources CFPrefs");
  858.     CFStringRef    applicationIDX = CFSTR("com.Windmill.MoreResources");
  859.     CFStringRef    applicationID;
  860.     CFStringRef    soundOnKey        = CFSTR("sound");
  861.     CFStringRef    fullScreenKey    = CFSTR("fullScreen");
  862.     CFStringRef    autoScrollKey    = CFSTR("autoScroll");
  863.     CFStringRef runCountKey        = CFSTR("runCount");
  864.     Boolean            booleanValue, success;
  865.  
  866.     if(gRunningOnX)
  867.         applicationID = applicationIDX;
  868.     else
  869.         applicationID = applicationID9;
  870.  
  871.     booleanValue = CFPreferencesGetAppBooleanValue(soundOnKey,applicationID,&success);
  872.     if(success)
  873.         gSoundCFPref = booleanValue;
  874.     else
  875.     {
  876.         gSoundCFPref             = true;
  877.         gFullScreenCFPref    = true;
  878.         gAutoScrollCFPref    = true;
  879.         doSavePreferencesCFPrefs();
  880.         return;
  881.     }
  882.  
  883.     booleanValue = CFPreferencesGetAppBooleanValue(fullScreenKey,applicationID,&success);
  884.     if(success)
  885.         gFullScreenCFPref = booleanValue;
  886.  
  887.     booleanValue = CFPreferencesGetAppBooleanValue(autoScrollKey,applicationID,&success);
  888.     if(success)
  889.         gAutoScrollCFPref = booleanValue;
  890.  
  891.     gProgramRunCountCFPref = CFPreferencesGetAppIntegerValue(runCountKey,applicationID,
  892.                                                                                                                      &success);
  893. }
  894.  
  895. // **************************************************************************** doCopyResource
  896.  
  897. OSErr  doCopyResource(ResType resType,SInt16 resID,SInt16 sourceFileRefNum,
  898.                                             SInt16 destFileRefNum)
  899. {
  900.     SInt16    oldResFileRefNum;
  901.     Handle    sourceResourceHdl;
  902.     ResType    ignoredType;
  903.     SInt16    ignoredID;
  904.     Str255     resourceName;
  905.     SInt16    resAttributes;
  906.     OSErr        osError;
  907.  
  908.     oldResFileRefNum = CurResFile();
  909.     UseResFile(sourceFileRefNum);
  910.  
  911.     sourceResourceHdl = Get1Resource(resType,resID);
  912.  
  913.     if(sourceResourceHdl != NULL)
  914.     {
  915.         GetResInfo(sourceResourceHdl,&ignoredID,&ignoredType,resourceName);
  916.         resAttributes = GetResAttrs(sourceResourceHdl);
  917.         DetachResource(sourceResourceHdl);
  918.         UseResFile(destFileRefNum);
  919.         if(ResError() == noErr)
  920.             AddResource(sourceResourceHdl,resType,resID,resourceName);
  921.         if(ResError() == noErr)
  922.             SetResAttrs(sourceResourceHdl,resAttributes);
  923.         if(ResError() == noErr)
  924.             ChangedResource(sourceResourceHdl);
  925.         if(ResError() == noErr)
  926.             WriteResource(sourceResourceHdl);
  927.     }
  928.  
  929.     osError = ResError();
  930.  
  931.     ReleaseResource(sourceResourceHdl);
  932.     UseResFile(oldResFileRefNum);
  933.  
  934.     return osError;
  935. }
  936.  
  937. // ***************************************************************** doSavePreferencesResource
  938.  
  939. void  doSavePreferencesResource(void)
  940. {
  941.     SInt16                    currentResFile;
  942.     appPrefsHandle    appPrefsHdl;
  943.     Handle                     existingResHdl;
  944.     Str255                     resourceName = "\pPreferences";
  945.  
  946.     if(gPrefsFileRefNum == -1)
  947.         return;
  948.  
  949.     currentResFile = CurResFile();
  950.  
  951.     appPrefsHdl = (appPrefsHandle) NewHandleClear(sizeof(appPrefs));
  952.  
  953.     HLock((Handle) appPrefsHdl);
  954.  
  955.     (*appPrefsHdl)->sound = gSoundPref;
  956.     (*appPrefsHdl)->fullScreen = gFullScreenPref;
  957.     (*appPrefsHdl)->autoScroll = gAutoScrollPref;
  958.     (*appPrefsHdl)->programRunCount = gProgramRunCountPref;         
  959.  
  960.     UseResFile(gPrefsFileRefNum);
  961.  
  962.     existingResHdl = Get1Resource(rTypePrefs,kPrefsID);
  963.     
  964.     if(existingResHdl != NULL)
  965.     {
  966.         RemoveResource(existingResHdl);
  967.         DisposeHandle(existingResHdl);
  968.         if(ResError() == noErr)
  969.             AddResource((Handle) appPrefsHdl,rTypePrefs,kPrefsID,resourceName);
  970.         if(ResError() == noErr)
  971.             WriteResource((Handle) appPrefsHdl);
  972.     }
  973.  
  974.     HUnlock((Handle) appPrefsHdl);
  975.  
  976.     ReleaseResource((Handle) appPrefsHdl);
  977.     UseResFile(currentResFile);
  978. }
  979.  
  980. // ****************************************************************** doSavePreferencesCFPrefs
  981.  
  982. void  doSavePreferencesCFPrefs(void)
  983. {
  984.     CFStringRef    applicationID9 = CFSTR("MoreResources CFPrefs");
  985.     CFStringRef    applicationIDX = CFSTR("com.Windmill.MoreResources");
  986.     CFStringRef    applicationID;
  987.     CFStringRef    soundOnKey        = CFSTR("sound");
  988.     CFStringRef    fullScreenKey    = CFSTR("fullScreen");
  989.     CFStringRef    autoScrollKey = CFSTR("autoScroll");
  990.     CFStringRef    yes                        = CFSTR("yes");
  991.     CFStringRef    no                        = CFSTR("no");
  992.     CFStringRef runCountKey        = CFSTR("runCount");
  993.     Str255            runCountPascalString;
  994.     CFStringRef runCountCFString;
  995.  
  996.     if(gRunningOnX)
  997.         applicationID = applicationIDX;
  998.     else
  999.         applicationID = applicationID9;
  1000.  
  1001.     if(gSoundCFPref)
  1002.         CFPreferencesSetAppValue(soundOnKey,yes,applicationID);
  1003.     else
  1004.         CFPreferencesSetAppValue(soundOnKey,no,applicationID);
  1005.         
  1006.     if(gFullScreenCFPref)
  1007.         CFPreferencesSetAppValue(fullScreenKey,yes,applicationID);
  1008.     else
  1009.         CFPreferencesSetAppValue(fullScreenKey,no,applicationID);
  1010.  
  1011.     if(gAutoScrollCFPref)
  1012.         CFPreferencesSetAppValue(autoScrollKey,yes,applicationID);
  1013.     else
  1014.         CFPreferencesSetAppValue(autoScrollKey,no,applicationID);
  1015.  
  1016.     NumToString((SInt32) gProgramRunCountCFPref,runCountPascalString);
  1017.     runCountCFString = CFStringCreateWithPascalString(NULL,runCountPascalString,
  1018.                                                             CFStringGetSystemEncoding());
  1019.     CFPreferencesSetAppValue(runCountKey,runCountCFString,applicationID);
  1020.  
  1021.     CFPreferencesAppSynchronize(applicationID);
  1022.  
  1023.     if(runCountCFString != NULL)
  1024.         CFRelease(runCountCFString);
  1025. }
  1026.  
  1027. // ********************************************************* doLoadAndSetWindowSizeAndPosition
  1028.  
  1029. void  doLoadAndSetWindowSizeAndPosition(WindowRef windowRef)
  1030. {
  1031.     docStructureHandle    docStrucHdl;
  1032.     SInt16                            fileRefNum;
  1033.     OSErr                                osError;
  1034.     windowSizePosHandle    windowSizePosHdl;
  1035.     Rect                                windowRect;
  1036.     SInt16                             frameWidth, titleBarHeight, menuBarHeight;
  1037.  
  1038.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  1039.     fileRefNum = FSpOpenResFile(&(*docStrucHdl)->fileFSSpec,fsRdWrPerm);
  1040.     if(fileRefNum < 0)
  1041.     {
  1042.         osError = ResError();
  1043.         doErrorAlert(osError);
  1044.         return;
  1045.     }
  1046.  
  1047.     windowSizePosHdl = (windowSizePosHandle) Get1Resource(rTypeWinSizePos,kWinSizePosID);
  1048.  
  1049.     if(windowSizePosHdl != NULL )
  1050.     {
  1051.         windowRect = (*windowSizePosHdl)->windowRect;
  1052.     }
  1053.     else
  1054.     {
  1055.         doGetFrameWidthAndTitleBarHeight(windowRef,&frameWidth,&titleBarHeight);
  1056.         GetThemeMenuBarHeight(&menuBarHeight);
  1057.         SetRect(&windowRect,frameWidth + 1,titleBarHeight + menuBarHeight + 1,
  1058.                         frameWidth + 400,titleBarHeight + menuBarHeight + 190);
  1059.     }
  1060.  
  1061.     MoveWindow(windowRef,windowRect.left,windowRect.top,false);
  1062.     SizeWindow(windowRef,windowRect.right - windowRect.left,windowRect.bottom - windowRect.top,
  1063.                          true);
  1064.  
  1065.     ReleaseResource((Handle) windowSizePosHdl);
  1066.     CloseResFile(fileRefNum);
  1067. }
  1068.  
  1069. // ********************************************************** doGetFrameWidthAndTitleBarHeight
  1070.  
  1071. void  doGetFrameWidthAndTitleBarHeight(WindowRef windowRef,SInt16 *frameWidth,
  1072.                                                                              SInt16 *titleBarHeight)
  1073. {
  1074.     RgnHandle    structureRegionHdl    = NewRgn();
  1075.     RgnHandle    contentRegionHdl        = NewRgn();
  1076.     Rect            structureRect, contentRect;
  1077.  
  1078.     GetWindowRegion(windowRef,kWindowStructureRgn,structureRegionHdl);
  1079.     GetRegionBounds(structureRegionHdl,&structureRect);
  1080.     GetWindowRegion(windowRef,kWindowContentRgn,contentRegionHdl);
  1081.     GetRegionBounds(contentRegionHdl,&contentRect);
  1082.  
  1083.     *frameWidth = contentRect.left - structureRect.left - 1;
  1084.     *titleBarHeight = contentRect.top - structureRect.top - 1;
  1085.  
  1086.     DisposeRgn(structureRegionHdl);
  1087.     DisposeRgn(contentRegionHdl);
  1088. }
  1089.  
  1090. // *************************************************************** doSaveWindowSizeAndPosition
  1091.  
  1092. void  doSaveWindowSizeAndPosition(WindowRef windowRef)
  1093. {
  1094.     docStructureHandle    docStrucHdl;
  1095.     Rect                                portRect;
  1096.     SInt16                            fileRefNum;
  1097.     OSErr                                osError;
  1098.     windowSizePos                windowSizePosStruct;
  1099.     windowSizePosHandle    windowSizePosHdl;
  1100.  
  1101.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  1102.     fileRefNum = FSpOpenResFile(&(*docStrucHdl)->fileFSSpec,fsRdWrPerm);
  1103.     if(fileRefNum < 0)
  1104.     {
  1105.         osError = ResError();
  1106.         doErrorAlert(osError);
  1107.         return;
  1108.     }
  1109.  
  1110.     GetWindowPortBounds(windowRef,&portRect);
  1111.     SetPortWindowPort(windowRef);
  1112.     LocalToGlobal(&topLeft(portRect));
  1113.     LocalToGlobal(&botRight(portRect));
  1114.     windowSizePosStruct.windowRect = portRect;
  1115.  
  1116.     windowSizePosHdl = (windowSizePosHandle) Get1Resource(rTypeWinSizePos,kWinSizePosID);
  1117.     if(windowSizePosHdl != NULL)
  1118.     {    
  1119.         **windowSizePosHdl = windowSizePosStruct;
  1120.         ChangedResource((Handle) windowSizePosHdl);
  1121.         osError = ResError();
  1122.         if(osError != noErr)
  1123.             doErrorAlert(osError);
  1124.     }
  1125.     else
  1126.     {
  1127.         windowSizePosHdl = (windowSizePosHandle) NewHandle(sizeof(windowSizePos));
  1128.         if(windowSizePosHdl != NULL)
  1129.         {
  1130.             **windowSizePosHdl = windowSizePosStruct;
  1131.             AddResource((Handle) windowSizePosHdl,rTypeWinSizePos,kWinSizePosID,
  1132.                                     "\pLast window size and position");
  1133.         }
  1134.     }
  1135.  
  1136.     if(windowSizePosHdl != NULL)
  1137.     {
  1138.         UpdateResFile(fileRefNum);
  1139.         osError = ResError();
  1140.         if(osError != noErr)
  1141.             doErrorAlert(osError);
  1142.  
  1143.         ReleaseResource((Handle) windowSizePosHdl);
  1144.     }
  1145.  
  1146.     CloseResFile(fileRefNum);
  1147. }
  1148.  
  1149. // *************************************************************************** doGetPageFormat
  1150.  
  1151. void  doGetPageFormat(WindowRef windowRef)
  1152. {
  1153.     docStructureHandle    docStrucHdl;
  1154.     SInt16                            fileRefNum;
  1155.     OSErr                                osError;
  1156.     Handle                            pageFormatResourceHdl = NULL;
  1157.     PMPageFormat                pageFormat    = kPMNoPageFormat;
  1158.     Boolean                            settingsChanged;
  1159.  
  1160.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  1161.     HLock((Handle) docStrucHdl);
  1162.  
  1163.     fileRefNum = FSpOpenResFile(&(*docStrucHdl)->fileFSSpec,fsRdWrPerm);
  1164.     if(fileRefNum < 0)
  1165.     {
  1166.         osError = ResError();
  1167.         doErrorAlert(osError);
  1168.         return;
  1169.     }
  1170.  
  1171.     pageFormatResourceHdl = Get1Resource(rPageFormat,kPageFormatID);
  1172.  
  1173.     if(pageFormatResourceHdl != NULL)
  1174.     {
  1175.         PMUnflattenPageFormat(pageFormatResourceHdl,&pageFormat);
  1176.         PMSessionValidatePageFormat(gPrintSession,pageFormat,&settingsChanged);
  1177.  
  1178.         DisposeHandle((*docStrucHdl)->pageFormatHdl);
  1179.         PMFlattenPageFormat(pageFormat,&((*docStrucHdl)->pageFormatHdl));
  1180.  
  1181.         if(pageFormat != kPMNoPageFormat)
  1182.             PMRelease(&pageFormat);
  1183.         ReleaseResource(pageFormatResourceHdl);
  1184.     }
  1185.  
  1186.     CloseResFile(fileRefNum);
  1187.  
  1188.     HUnlock((Handle) docStrucHdl);
  1189. }
  1190.  
  1191. // ************************************************************************** doSavePageFormat
  1192.  
  1193. void  doSavePageFormat(WindowRef windowRef)
  1194. {
  1195.     docStructureHandle    docStrucHdl;
  1196.     Handle                            pageFormatHdl;
  1197.     SInt16                            fileRefNum;
  1198.     OSErr                                osError;
  1199.     Size                                handleSize;
  1200.  
  1201.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  1202.     handleSize = GetHandleSize((*docStrucHdl)->pageFormatHdl);
  1203.  
  1204.     fileRefNum = FSpOpenResFile(&(*docStrucHdl)->fileFSSpec,fsRdWrPerm);
  1205.     if(fileRefNum < 0)
  1206.     {
  1207.         osError = ResError();
  1208.         doErrorAlert(osError);
  1209.         return;
  1210.     }
  1211.  
  1212.     pageFormatHdl = Get1Resource(rPageFormat,kPageFormatID);
  1213.  
  1214.     if(pageFormatHdl != NULL)
  1215.     {
  1216.         RemoveResource(pageFormatHdl);
  1217.         DisposeHandle(pageFormatHdl);
  1218.         pageFormatHdl = NewHandle(handleSize);
  1219.         BlockMove(*((*docStrucHdl)->pageFormatHdl),*pageFormatHdl,handleSize);
  1220.         AddResource(pageFormatHdl,rPageFormat,kPageFormatID,"\pPage Format");
  1221.         osError = ResError();
  1222.         if(osError != noErr)
  1223.             doErrorAlert(osError);
  1224.     }
  1225.     else
  1226.     {
  1227.         pageFormatHdl = NewHandle(handleSize);
  1228.         if(pageFormatHdl != NULL)
  1229.         {
  1230.             BlockMove(*((*docStrucHdl)->pageFormatHdl),*pageFormatHdl,handleSize);
  1231.             AddResource(pageFormatHdl,rPageFormat,kPageFormatID,"\pPage Format");
  1232.             osError = ResError();
  1233.             if(osError != noErr)
  1234.                 doErrorAlert(osError);
  1235.         }
  1236.     }
  1237.  
  1238.     if(pageFormatHdl != NULL)
  1239.     {
  1240.         UpdateResFile(fileRefNum);
  1241.         osError = ResError();
  1242.         if(osError != noErr)
  1243.             doErrorAlert(osError);
  1244.  
  1245.         ReleaseResource(pageFormatHdl);
  1246.     }
  1247.  
  1248.     gPageFormatChanged = false;
  1249.  
  1250.     CloseResFile(fileRefNum);
  1251. }
  1252.  
  1253. // *******************************************************************************************
  1254.  
  1255.